package com.hospital.xml;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;

import android.util.Xml;

public class PullXmlParser {

	public IXmlParser iXmlParser;

	public static final int StartTag = XmlPullParser.START_TAG;

	public static final int EndTag = XmlPullParser.END_TAG;

	public void parse(InputStream is) throws Exception {

		XmlPullParser parser = Xml.newPullParser(); // 由android.util.Xml创建一个XmlPullParser实例
		parser.setInput(is, "UTF-8"); // 设置输入流 并指明编码方式

		int eventType = parser.getEventType();
		while (eventType != XmlPullParser.END_DOCUMENT) {
			switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					iXmlParser.Stardoc();
					break;
				case PullXmlParser.StartTag:
					iXmlParser.Startevent(eventType, parser);
					break;
				case PullXmlParser.EndTag:
					iXmlParser.Endevent(eventType, parser);
					break;
				default:
					iXmlParser.Doevent(eventType, parser);
					break;
			}
			eventType = parser.next();
		}
		iXmlParser.Enddoc();
	}

	public void parse(String str) throws Exception {

		parse(new ByteArrayInputStream(str.getBytes()));

	}

	public interface IXmlParser {
		// 开始读取
		void Stardoc() throws Exception;

		void Enddoc() throws Exception;

		void Endevent(int eventType, XmlPullParser parser) throws Exception;

		void Startevent(int eventType, XmlPullParser parser) throws Exception;

		// 处理数据
		void Doevent(int eventType, XmlPullParser parser) throws Exception;
	}
}
